package learningjava;

import java.awt.event.*;
import java.beans.Statement;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;


public class DBaseFrame extends javax.swing.JFrame {

    static String rez = null;
    String p,ss;
    String k;
    
    javax.swing.table.DefaultTableModel dtm; 
    javax.swing.DefaultComboBoxModel dcbm;
    OracleCC occ = new OracleCC();
    
    public DBaseFrame() {
      initComponents();
      //один из вариантов работы с таблицей
      dtm=new javax.swing.table.DefaultTableModel();
      dcbm=new javax.swing.DefaultComboBoxModel();
      bdTable.setModel(dtm);
      tableList.setModel(dcbm);
    //  dtm.addColumn("Наименование");
      //
      
    }
    
    void Update()
    {
     occ.ReadSQLtoDCBM(ConnectParam.DBCurrentPath,"select * from user_tables", dcbm);   
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton4 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        bdTable = new javax.swing.JTable();
        tableList = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();
        getBtnTabl = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        resText = new javax.swing.JTextField();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton4.setText("jButton4");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("jLabel1");
        jLabel1.setAutoscrolls(true);
        jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        jLabel2.setText("jLabel2");

        bdTable.setModel(new javax.swing.table.DefaultTableModel(){});
        bdTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        bdTable.setAutoscrolls(false);
        bdTable.setName("");
        bdTable.setRowSelectionAllowed(false);
        bdTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        bdTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bdTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(bdTable);

        tableList.setModel(new javax.swing.DefaultComboBoxModel(new String[] {}));
        tableList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tableListActionPerformed(evt);
            }
        });

        jButton2.setText("jButton2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        getBtnTabl.setText("Содержимое таблицы");
        getBtnTabl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getBtnTablActionPerformed(evt);
            }
        });

        jButton3.setText("Обновление");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        resText.setName("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(getBtnTabl)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                        .addComponent(tableList, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(resText)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tableList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(getBtnTabl)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(resText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        occ.ReadSQLtoDTM(ConnectParam.DBCurrentPath,"select * from user_tables", dtm);      
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tableListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tableListActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tableListActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
      /*  
        tableList.addActionListener(new ActionListener() { 
                    
            public void actionPerformed(ActionEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
                Object ob=dcbm.getSelectedItem();
                System.out.println(ob.toString());
                 k=ob.toString();
            }
            }
   );
            
       String s="select * from "+k;
       occ.ReadSQLtoDTM(ConnectParam.DBCurrentPath,s,dtm); 
        */
    }//GEN-LAST:event_jButton2ActionPerformed

    private void getBtnTablActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getBtnTablActionPerformed
        dtm.setNumRows(0); //очищаем модель таблицы
       
        occ.ReadSQLtoDTM(ConnectParam.DBCurrentPath,"select t.rowid,t.*  from "+tableList.getSelectedItem().toString()+" t",dtm); //передаем в модель данные с бд 
  ////      occ.ReadSQLtoDTM1(ConnectParam.DBCurrentPath,"select *  from "+tableList.getSelectedItem().toString(),"select DBMS_ROWID.ROWID_ROW_NUMBER(rowid) from "+tableList.getSelectedItem().toString(),dtm);
    }//GEN-LAST:event_getBtnTablActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //occ.ReadSQLtoDTM(rez, rez, dtm);
        
       /* int Column,Row;
      bdTable.getModel().addTableModelListener(new TableModelListener(){
       public void tableChanged(TableModelEvent e){
           System.out.println("Column"+e.getColumn()); 
           System.out.println("Row"+e.getFirstRow());
           
           occ.ReadSQLtoDTM(ConnectParam.DBCurrentPath,"select * from "+tableList.getSelectedItem().toString()+"where ",dtm);
           
       }
      });
     * 
     */
    }//GEN-LAST:event_jButton3ActionPerformed

    private void bdTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bdTableMouseClicked
    //    System.out.println("Выбран столбец "+bdTable.getColumnName(bdTable.getSelectedColumn())+" со значением "+bdTable.getValueAt(bdTable.getSelectedRow(), bdTable.getSelectedColumn())+" в таблице "+tableList.getSelectedItem().toString());
     if(bdTable.getSelectedColumn()!=0) //нам не надо модифицировать этот столбец
        resText.setText("UPDATE "+tableList.getSelectedItem().toString()+" SET "+bdTable.getColumnName(bdTable.getSelectedColumn())+"='"+bdTable.getValueAt(bdTable.getSelectedRow(), bdTable.getSelectedColumn())+"' where ROWID="+bdTable.getValueAt(bdTable.getSelectedRow(), 0)); //здесь вместо жесткого индекса надо сделать поиск по наименованию столбца
     else
        resText.setText(""); 
     // String s=bdTable.getColumnName(bdTable.getSelectedColumn());
       // Object s1=bdTable.getValueAt(bdTable.getSelectedRow(),bdTable.getSelectedColumn());
       // occ.Read(ConnectParam.DBCurrentPath,"select rowid from "+tableList.getSelectedItem().toString(),dtm);
                //" where s ="+ bdTable.getValueAt(bdTable.getSelectedRow(), bdTable.getSelectedColumn()));
      //  occ.ReadSQLtoDTM(ConnectParam.DBCurrentPath,"select * from "+tableList.getSelectedItem().toString(),dtm);
        
        
    }//GEN-LAST:event_bdTableMouseClicked

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable bdTable;
    private javax.swing.JButton getBtnTabl;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField resText;
    private javax.swing.JComboBox tableList;
    // End of variables declaration//GEN-END:variables
}
